﻿-- Author      : Kurapica

--------------------------
-- Selector                 --
--------------------------
do
	IGAS:NewAddon("IGAS_Studio")
    
    -- Base Form for all other forms in Studio    
    local _WidgetName = "Selector"
    local _Base = "Frame"
    
    -- Script
    -- Only one selector would be created.
    local selector
    local stF
    local stX, stY, endX, endY
    
    local _FrameBackdrop = {
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}
    
    local function OnUpdate(self, elasped)
        endX, endY = GetCursorPosition()
        if abs(stX-endX) > 6 or abs(stY - endY) > 6 then
            selector:SetBackdrop(_FrameBackdrop)
        end
        if selector.Visible then
            selector:SetPoint("TOPRIGHT", selector.Parent, "BOTTOMLEFT", max(stX, endX), max(stY, endY))
            selector:SetPoint("BOTTOMLEFT", selector.Parent, "BOTTOMLEFT", min(stX, endX), min(stY, endY))
        end
    end
    
    local function OnMouseDown(self, ...)
		if not selector.__InSelectedMode then
			return
		end
        stF = self
        stX, stY = GetCursorPosition()
        endX, endY = stX, stY
        selector:SetBackdrop(nil)
        selector.OnUpdate = OnUpdate
        selector.Visible = true
    end
    
    local function OnMouseUp(self, ...)
		if not selector.__InSelectedMode then
			return
		end
        selector.OnUpdate = nil
        selector.Visible = false
        if abs(stX-endX) > 6 or abs(stY - endY) > 6 then
            selector:Fire("OnAreaSelected", stF, floor(min(stX, endX)), floor(max(stY, endY)), floor(max(stX, endX)), floor(min(stY, endY)))
        end
    end
	
    -- ScriptType
    local _ScriptType = {
        ["OnAreaSelected"] = true,        
    }
    
    --    Function
    local _Function = {
        -- HookFrame
        ["HookFrame"] = function(self, frame)
			frame.__UI:EnableMouse(true)
            --frame:StackScript("OnMouseDown", OnMouseDown)
            --frame:StackScript("OnMouseUp", OnMouseUp)
			frame.OnMouseDown = OnMouseDown
			frame.OnMouseUp = OnMouseUp
        end,
    }
    
    --    Property
    local _Property = {
		-- InSelectedMode
		["InSelectedMode"] = {
			["Set"] = function(self, flag)
				self.__InSelectedMode = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return (self.__InSelectedMode and true) or false
			end,
			
			["Type"] = IGAS:GetPropertyType("Boolean"),
		},
    }
    
    local function _New(name, parent)
		-- Only One selector will be exits, so....
        if selector then
            return selector
        end
        
        selector = CreateFrame(_Base, name, parent)
		selector.FrameStrata = "FULLSCREEN"
        selector.TopLevel = true
        selector:SetBackdrop(_FrameBackdrop)
		
		local t = CreateFrame("Texture", "MaskColor", selector)
		t:SetTexture(1, 1, 1, 0.5)
		t:SetAllPoints(selector)
        
        return selector
    end    
    
    -- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["Function"] = _Function,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
